package com.kim.nidoya.activity;

import java.io.InputStream;

import org.json.JSONObject;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.kim.net.NetCallback;
import com.kim.net.NetEntity;
import com.kim.net.impl.NetProcessProxy;
import com.kim.nidoya.R;
import com.kim.nidoya.domain.constant.UserConstant;
import com.kim.nidoya.net.URLConstant;
import com.kim.nidoya.net.business.DataTest;
import com.kim.nidoya.net.business.LoginBusiness;
import com.kim.nidoya.net.business.NetCallbackImplGetBase;
import com.kim.nidoya.net.business.NetCallbackImplBase.Notify;
import com.kim.nidoya.net.business.NetCallbackImplGetBase.NotifyGet;
import com.kim.nidoya.net.business.RequestGetBusiness;
import com.kim.nidoya.net.entity.LoginEntity;

/**
 * 登录页
 * 
 * @author Kim
 * 
 */
public class LoginActivity extends BaseActivity {
	private EditText edt_userName;
	private EditText edt_passWord;

	private myHandler handler;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		handler = new myHandler();
	}

	@Override
	protected void initParam() {
		// System.out.println("initparam");
		//
		// NotifyGet notify = new NotifyGet() {
		//
		// @Override
		// public void notifyNetCallback(int httpResponseCode, String result) {
		// Log.d("LoginActivity", "result::" + result);
		//
		// }
		//
		// @Override
		// public void notifyError(int httpResponseCode, String errorMsg) {
		// Log.d("LoginActivity", "errorMsg::" + httpResponseCode + ":"
		// + errorMsg);
		// }
		// };
		//
		// RequestGetBusiness.doGetRequest(URLConstant.URL_LOGIN + "?LoginName="
		// + DataTest.userName + "&Password=" + DataTest.password, notify);
		// // LoginBusiness business = new LoginBusiness(notify,
		// DataTest.userName,
		// // DataTest.password);
		// // business.doBusiness();

	}

	@Override
	protected void initView() {
		setContentView(R.layout.login_layout);
		((Button) findViewById(R.id.btn_register)).setOnClickListener(this);
		((Button) findViewById(R.id.btn_login)).setOnClickListener(this);
		edt_userName = (EditText) findViewById(R.id.edt_username);
		edt_passWord = (EditText) findViewById(R.id.edt_password);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_register:
			toRegister();
			break;
		case R.id.btn_login:
			// toMain();
			String userName = edt_userName.getText().toString();
			String passWord = edt_passWord.getText().toString();
			loginReques(userName, passWord);
			break;
		}
	}

	/**
	 * 登录请求
	 * 
	 * @param userName
	 * @param passWord
	 */
	private void loginReques(String userName, String passWord) {
		// 测试账号
		userName = DataTest.userName;
		passWord = DataTest.password;

		NotifyGet notify = new NotifyGet() {
			@Override
			public void notifyNetCallback(int httpResponseCode, String result) {
				Log.d("LoginActivity", "result::" + result);
				if (result == null || result.equals("")) {//登录失败，提示用户
					Message msg = new Message();
					msg.what = UserConstant.LOGIN_FALSE;
					handler.sendMessage(msg);
				} else {
					toMain();// 登录成功，跳到首页
				}
			}

			@Override
			public void notifyError(int httpResponseCode, String errorMsg) {
				Log.d("LoginActivity", "errorMsg::" + httpResponseCode + ":"
						+ errorMsg);
				Message msg = new Message();//登录失败，提示用户
				msg.what = UserConstant.LOGIN_FALSE;
				handler.sendMessage(msg);
			}
		};

		RequestGetBusiness.doGetRequest(URLConstant.URL_LOGIN + "?LoginName="
				+ userName + "&Password=" + passWord, notify);

	}

	/**
	 * 跳转到登录页
	 */
	private void toRegister() {
		Intent intent = new Intent();
		intent.setClass(this, RegisterActivity.class);
		startActivity(intent);
	}

	class myHandler extends Handler {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			if (msg.what == UserConstant.LOGIN_FALSE) {
				Toast.makeText(LoginActivity.this, R.string.login_false,
						Toast.LENGTH_SHORT).show();// 登录失败
			}
		}
	}

	/**
	 * 跳转到首页
	 */
	private void toMain() {
		Intent intent = new Intent();
		intent.setClass(this, TabMainActivity.class);
		startActivity(intent);

		finish();
	}

}
